package com.chonor.mall.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.chonor.mall.dao.ResourceDao;
import com.chonor.mall.mapper.ResourceMapper;
import com.chonor.mall.model.Resource;
import com.chonor.mall.model.ResourceExample;
import com.chonor.mall.service.AdminCacheService;
import com.chonor.mall.service.ResourceService;
import com.chonor.mall.vo.ResourceVO;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class ResourceServiceImpl implements ResourceService {

    @Autowired
    private ResourceMapper resourceMapper;

    @Autowired
    private AdminCacheService adminCacheService;

    @Autowired
    private ResourceDao resourceDao;

    @Override
    public int create(Resource resource) {
        resource.setCreateat(new Date());
        return resourceMapper.insert(resource);
    }

    @Override
    public int update(Long id, Resource resource) {
        resource.setId(id);
        int count = resourceMapper.updateByPrimaryKeySelective(resource);
        adminCacheService.delResourceListByResource(id);
        return count;
    }

    @Override
    public Resource getItem(Long id) {
        return resourceMapper.selectByPrimaryKey(id);
    }

    @Override
    public int delete(Long id) {
        int count = resourceMapper.deleteByPrimaryKey(id);
        adminCacheService.delResourceListByResource(id);
        return count;
    }

    @Override
    public List<Resource> list(Long categoryId, String nameKeyword, String urlKeyword, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        ResourceExample example = new ResourceExample();
        ResourceExample.Criteria criteria = example.createCriteria();
        if(categoryId!=null){
            criteria.andCategoryIdEqualTo(categoryId);
        }
        if(StrUtil.isNotEmpty(nameKeyword)){
            criteria.andNameLike('%'+nameKeyword+'%');
        }
        if(StrUtil.isNotEmpty(urlKeyword)){
            criteria.andUrlLike('%'+urlKeyword+'%');
        }
        return resourceMapper.selectByExample(example);
    }

    @Override
    public List<Resource> listAll() {
        return resourceMapper.selectByExample(new ResourceExample());
    }

    @Override
    public List<ResourceVO> getResourceVO() {
        List<ResourceVO> resources = resourceDao.getList();
        if(Objects.isNull(resources) || resources.size() < 1) return null;
        return resources;
    }

//    private List<ResourceVO> convertResource(List<Resource> resources){
//
//        List<ResourceVO> resourceVOS = new ArrayList<>(resources.size());
//        for (Resource resource : resources) {
//            ResourceVO resourceVO = new ResourceVO();
//            BeanUtils.copyProperties(resource, resourceVO);
//            resourceVO.setCreateat(DateUtil.formatDate(resource.getCreateat()));
//
//            ResourceVO.ResourceCategory resourceCategory = new ResourceVO.ResourceCategory();
//            resourceCategory.setId(resource.getCategoryId());
//
//
//        }
//    }
}
